package cn.tedu.mall.seckill.consumer;

import cn.tedu.mall.pojo.seckill.model.Success;
import cn.tedu.mall.seckill.config.RabbitMqComponentConfiguration;
import cn.tedu.mall.seckill.mapper.SeckillSkuMapper;
import cn.tedu.mall.seckill.mapper.SuccessMapper;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = {RabbitMqComponentConfiguration.SECKILL_QUEUE})
public class SeckillQueueConsumer {
    //需要mapper中的方法
    @Autowired
    private SuccessMapper successMapper;
    @Autowired
    private SeckillSkuMapper seckillSkuMapper;

    @RabbitHandler
    public void process(Success success){
        //先减少库存
        //秒杀活动结束之后减少的。所以不会影响到秒杀活动
        seckillSkuMapper.updateReducesStockBySkuId(success.getSkuId(),success.getQuantity());
        //新增success对象到数据库
        successMapper.saveSuccess(success);
    }
}
